<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/2/12 0012
 * Time: 0:32
 */

namespace app\index\controller;


use think\Controller;
use think\Db;
use app\index\model\ToolApp as ToolAppModel;
use app\index\model\Tool as ToolModel;
use think\Session;
use think\Validate;

class ToolApp extends Controller
{
    protected $middleware = [
        'UserCheck'
    ];
    //小型器具租借
    public function tool_small_app(){
        $tool=new ToolModel();
    	$tools=$tool->where(
    	    [
    	        'statu'=>0,
                'type' =>0
            ])->paginate(10);
    	$this->assign('tools',$tools);
        return $this->fetch();
    }
    //租借
    public function tool_choose($id){
        if ($this->request->isPost()){
            $data=input('post.');
            $tool_app=new ToolAppModel();
            $rule=[
                'starttime|开始时间'=>'require',
                'time|借出天数'     =>'require',
            ];
            $validate=new Validate();
            $va=$validate->check($data,$rule);
            if ($va==true){
                $tool_app=$tool_app->save([
                    'tool_id'=>$id,
                    'user_id'=>\session('user')['id'],
                    'starttime'=>$data['starttime'],
                    'time'    =>$data['time'],
                    'statu'   =>0
                ]);
                if ($tool_app){
                    $tool=ToolModel::get($id);
                    $lab=$tool->lab()->find();
                    $teacher=$tool->teacher()->find();
                    $email=new Mail();
                    $email_send=$email
                        ->send_mail(
                            $teacher['email'],
                            '广州商学院实验室',
                            '您'.$lab['name'].'课室下的'.$tool['name'].'被申请预约,请前去审批');
                    $msg=$this->msg(200,'申请成功');
                }else{
                    $msg=$this->msg(400,'申请失败');
                }
            }else{
                $msg=$this->msg(400,$validate->getError());
            }

            return $msg;
        }
        $tool=new ToolModel();
        $tool=$tool->where('id',$id)->find();
        $tishi=Db::table('tool_app')->where([
            'tool_id'=>$id,
            'statu'  =>0
        ])->count();
        $this->assign('tishi',$tishi);
        $this->assign('tool',$tool);
        return $this->fetch();
    }
    //大型器具租借
    public function tool_big_app(){
        $tool=new ToolModel();
        $tools=$tool->where(
            [
                'statu'=>0,
                'type' =>1
            ])->paginate(10);
        $this->assign('tools',$tools);
        return $this->fetch();
    }
    //用户当前预约记录
    public function tool_list(){
        $user_id=\session('user')['id'];
        $tool_app=new ToolAppModel();
        $tool_apps=$tool_app
            ->where('user_id',$user_id)
            ->order('create_time','desc')
            ->paginate(10);
        $this->assign('tools',$tool_apps);
        return $this->fetch();
    }
    //信息
    public function msg($statu,$msg){
        return [
            'statu'=>$statu,
            'msg'  =>$msg
        ];
    }
}